package com.hilaver.dzmis.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.CtClassName;
import com.hilaver.dzmis.dao.BaseHibernateDAO;
import com.hilaver.dzmis.service.II18nService;
import com.hilaver.dzmis.sys.SysMenu;
import com.hilaver.dzmis.sys.SysMessage;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class I18nServiceImpl implements II18nService {

	private BaseHibernateDAO dao;

	public I18nServiceImpl() {
		this.dao = new BaseHibernateDAO();
	}

	@Override
	public String getMessage(Locale locale) {
		return null;
	}

	@Override
	public String getMessage(Locale locale, String keyPrefix) throws Exception {
		String key;
		if (keyPrefix == null || "".equals(keyPrefix)) {
			keyPrefix = "";
			key = locale.toString().toLowerCase();
		} else {
			key = keyPrefix + Constants.KEY_SEPARATOR + locale.toString().toLowerCase();
		}
		
//		if (I18N_MAP.containsKey(key)) {
//			return I18N_MAP.get(key);
//		}

		SysMessage exampleMessage = new SysMessage();
		exampleMessage.setKeyCode(keyPrefix + "%");
		List messageList = this.dao.findByExample(exampleMessage, "keyCode", Constants.ORDER_DESC);

		StringBuffer xmlSB = new StringBuffer();

		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_LOCALE_TAG[0]);
		xmlSB.append(locale.toString().toLowerCase());
		xmlSB.append(Constants.XML_LOCALE_TAG[1]);
		for (Iterator iterator = messageList.iterator(); iterator.hasNext();) {
			SysMessage sysMessage = (SysMessage)iterator.next();
			xmlSB.append("<" + sysMessage.getKeyCode() + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			
			if(Constants.LOCALE_TYPE_EN.equals(locale)){
				xmlSB.append(sysMessage.getDescriptionEn());
			}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
				xmlSB.append(sysMessage.getDescriptionCn());
			}else{
				xmlSB.append(sysMessage.getDescriptionFr());
			}
			
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + sysMessage.getKeyCode() + ">");
		}
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		I18N_MAP.put(key, xmlSB.toString());
		return xmlSB.toString();
	}
	
	public String getShortMessage(Locale locale, String keyCode) throws Exception {
		SysMessage exampleMessage = new SysMessage();
		exampleMessage.setKeyCode(keyCode);
		List messageList = this.dao.findByExample(exampleMessage, "keyCode", Constants.ORDER_DESC);
		if(messageList.size() > 0){
			SysMessage message = (SysMessage)messageList.get(0);
			String rtn;
			if(Constants.LOCALE_TYPE_EN.equals(locale)){
				rtn = message.getDescriptionEn();
			}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
				rtn = message.getDescriptionCn();
			}else{
				rtn = message.getDescriptionFr();
			}
			return rtn;
		}else{
			return "";
		}
	}

	@Override
	public String getMenu(Locale locale, String menuType) throws Exception {
		String key;
		if(menuType == null || "".equals(menuType)){
			menuType = "";
			key = locale.toString().toLowerCase();;
		}else{
			key = menuType + Constants.KEY_SEPARATOR + locale.toString().toLowerCase();
		}
		
		if(MENU_MAP.containsKey(key)){
			return MENU_MAP.get(key);
		}
		
		SysMenu exampleMenu = new SysMenu();
		exampleMenu.setType(menuType);
		exampleMenu.setIsFirstLevel(IS_FIRSTLEVEL_MENU);
		
		List menuList = this.dao.findByExample(exampleMenu, "sequence", Constants.ORDER_ASC);
		StringBuffer xmlSB = new StringBuffer();

		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		//Menu
		for (Iterator iterator1 = menuList.iterator(); iterator1.hasNext();) {
			SysMenu sysMenu = (SysMenu)iterator1.next();
			xmlSB.append("<menu id=\"");
			xmlSB.append(sysMenu.getId());
			xmlSB.append("\" ");
			xmlSB.append("description=\"");
			
			if(Constants.LOCALE_TYPE_EN.equals(locale)){
				xmlSB.append(sysMenu.getDescriptionEn());
			}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
				xmlSB.append(sysMenu.getDescriptionCn());
			}else{
				xmlSB.append(sysMenu.getDescriptionFr());
			}
			
			xmlSB.append("\">");
			
			//Sub menu
			Set subMenuList = sysMenu.getSysMenus();
			for(Iterator iterator2 = subMenuList.iterator(); iterator2.hasNext();){
				SysMenu subMenu = (SysMenu)iterator2.next();
				xmlSB.append("<subMenu id=\"");
				xmlSB.append(subMenu.getId());
				xmlSB.append("\" ");
				xmlSB.append("description=\"");
			
				if(Constants.LOCALE_TYPE_EN.equals(locale)){
					xmlSB.append(subMenu.getDescriptionEn());
				}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
					xmlSB.append(subMenu.getDescriptionCn());
				}else{
					xmlSB.append(subMenu.getDescriptionFr());
				}
			
				xmlSB.append("\" actionURL=\"");
			
				xmlSB.append(subMenu.getActionUrl());
			
				xmlSB.append("\"/>");
			}
			
			xmlSB.append("</menu>");
		}
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		MENU_MAP.put(key, xmlSB.toString());
		
		return xmlSB.toString();
	}
	
	@Override
	public String getMessage(int page, String sort, String order) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<SysMessage> messageList = this.dao.getAllPagination(SysMessage.class.getName(), offset, Constants.PAGE_SIZE, sort, order);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (SysMessage sysMessage: messageList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++ offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(sysMessage));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(SysMessage.class.getName()));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public static void main(String[] args) throws Exception {
		BaseHibernateDAO dao = new BaseHibernateDAO();
		SysMessage exampleMessage = new SysMessage();
		exampleMessage.setKeyCode("com.hilaver.dzmis%");
		List<SysMessage> messageList = dao.findByExample(exampleMessage, "keyCode", Constants.ORDER_DESC);
		for (SysMessage sysMessage : messageList) {
			CtClassName ctClassName = new CtClassName();
			ctClassName.setCode(sysMessage.getKeyCode());
			ctClassName.setDescriptionCn(sysMessage.getDescriptionCn());
			ctClassName.setDescriptionEn(sysMessage.getDescriptionEn());
			ctClassName.setDescriptionFr(sysMessage.getDescriptionFr());
			dao.saveOrUpdate(ctClassName);
		}
		
	}
	
}
